package cn.iocoder.yudao.module.biz.service.customer.serviceUnit;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.biz.controller.admin.customer.serviceUnit.vo.*;
import cn.iocoder.yudao.module.biz.dal.dataobject.customer.serviceUnit.ServiceUnitDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.biz.dal.mysql.customer.serviceUnit.ServiceUnitMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.module.biz.enums.ErrorCodeConstants.*;

/**
 * 客户档案-服务单元管理 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class ServiceUnitServiceImpl implements ServiceUnitService {

    @Resource
    private ServiceUnitMapper serviceUnitMapper;

    @Override
    public Long createServiceUnit(ServiceUnitSaveReqVO createReqVO) {
        // 插入
        ServiceUnitDO customerArchiveServiceUnit = BeanUtils.toBean(createReqVO, ServiceUnitDO.class);
        serviceUnitMapper.insert(customerArchiveServiceUnit);

        // 返回
        return customerArchiveServiceUnit.getId();
    }

    @Override
    public void updateServiceUnit(ServiceUnitSaveReqVO updateReqVO) {
        // 校验存在
        validateServiceUnitExists(updateReqVO.getId());
        // 更新
        ServiceUnitDO updateObj = BeanUtils.toBean(updateReqVO, ServiceUnitDO.class);
        serviceUnitMapper.updateById(updateObj);
    }

    @Override
    public void deleteServiceUnit(Long id) {
        // 校验存在
        validateServiceUnitExists(id);
        // 删除
        serviceUnitMapper.deleteById(id);
    }

    @Override
        public void deleteServiceUnitListByIds(List<Long> ids) {
        // 删除
        serviceUnitMapper.deleteByIds(ids);
        }


    private void validateServiceUnitExists(Long id) {
        if (serviceUnitMapper.selectById(id) == null) {
            throw exception(BIZ_CUSTOMER_ARCHIVE_SERVICE_UNIT_NOT_EXISTS);
        }
    }

    @Override
    public ServiceUnitDO getServiceUnit(Long id) {
        return serviceUnitMapper.selectById(id);
    }

    @Override
    public PageResult<ServiceUnitDO> getServiceUnitPage(ServiceUnitPageReqVO pageReqVO) {
        return serviceUnitMapper.selectPage(pageReqVO);
    }

     @Override
    public List<Map<String,Object>> getSimpleList(ServiceUnitPageReqVO pageReqVO) {
        return serviceUnitMapper.getSimpleList(pageReqVO);
    }
}